package com.vdg.lockphotos;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.SubMenu;
import com.vdg.lockphotos.data.Photo;
import com.vdg.lockphotos.fragment.HomeFragment;
import com.vdg.lockphotos.fragment.ProductDetailFragment;
import com.vdg.lockphotos.notify.OnSelectMenuItemListener;

public class DisplayImageActivity extends SherlockFragmentActivity {

	private Fragment contentFragment;
	HomeFragment homeFragment;
	ProductDetailFragment pdtDetailFragment;
	private OnSelectMenuItemListener mListener;
	public static final String DISPLAY_IMAGE_URL_KEY = "url_key";

	public String getDisplayUrl() {
		if (getIntent() != null
				&& getIntent().getStringExtra(DISPLAY_IMAGE_URL_KEY) != null) {
			return getIntent().getStringExtra(DISPLAY_IMAGE_URL_KEY);
		}
		return "";
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		setTheme(R.style.Theme_Sherlock_Light_DarkActionBar);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.display_image_layout);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setHomeButtonEnabled(true);
		getSupportActionBar().setBackgroundDrawable(
				new ColorDrawable(getResources()
						.getColor(R.color.top_bar_color)));
		FragmentManager fragmentManager = getSupportFragmentManager();

		if (savedInstanceState != null) {
			if (savedInstanceState.containsKey("content")) {
				String content = savedInstanceState.getString("content");
				if (content.equals(ProductDetailFragment.ARG_ITEM_ID)) {
					if (fragmentManager
							.findFragmentByTag(ProductDetailFragment.ARG_ITEM_ID) != null) {
						contentFragment = fragmentManager
								.findFragmentByTag(ProductDetailFragment.ARG_ITEM_ID);
					}
				}
			}
			if (fragmentManager.findFragmentByTag(HomeFragment.ARG_ITEM_ID) != null) {
				homeFragment = (HomeFragment) fragmentManager
						.findFragmentByTag(HomeFragment.ARG_ITEM_ID);
				contentFragment = homeFragment;
			}
		} else {
			homeFragment = new HomeFragment();
			switchContent(homeFragment, HomeFragment.ARG_ITEM_ID);
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		if (contentFragment instanceof HomeFragment) {
			outState.putString("content", HomeFragment.ARG_ITEM_ID);
		} else {
			outState.putString("content", ProductDetailFragment.ARG_ITEM_ID);
		}
		super.onSaveInstanceState(outState);
	}

	public void switchContent(Fragment fragment, String tag) {
		FragmentManager fragmentManager = getSupportFragmentManager();
		while (fragmentManager.popBackStackImmediate())
			;

		if (fragment != null) {
			FragmentTransaction transaction = fragmentManager
					.beginTransaction();
			transaction.replace(R.id.content_frame, fragment, tag);
			// Only ProductDetailFragment is added to the back stack.
			if (!(fragment instanceof HomeFragment)) {
				transaction.addToBackStack(tag);
			}
			transaction.commit();
			contentFragment = fragment;
		}
	}

	@Override
	public void onBackPressed() {
		FragmentManager fm = getSupportFragmentManager();
		if (fm.getBackStackEntryCount() > 0) {
			super.onBackPressed();
		} else if (contentFragment instanceof HomeFragment
				|| fm.getBackStackEntryCount() == 0) {
			finish();
		}
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
		if (item.getTitle().equals(getResources().getString(R.string.un_hide))) {
			if (mListener != null)
				mListener.onSelectUnHide();
		}
		if (item.getTitle().equals(
				getResources().getString(R.string.view_this_photo))) {
			if (mListener != null)
				mListener.onSelectViewImage();
		}
		switch (item.getItemId()) {
		case android.R.id.home:
			// app icon in action bar clicked; go home
			onBackPressed();
			break;
		}
		return super.onMenuItemSelected(featureId, item);
	}

	public void regisOnSelectMenuListener(OnSelectMenuItemListener listener) {
		mListener = listener;
	}

	@Override
	public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
		// TODO Auto-generated method stub
		SubMenu subMenu1 = menu.addSubMenu(getResources().getString(
				R.string.menu));
		subMenu1.add(getResources().getString(R.string.un_hide));
		subMenu1.add(getResources().getString(R.string.view_this_photo));
		MenuItem subMenu1Item = subMenu1.getItem();
		subMenu1Item.setIcon(R.drawable.ic_action_overflow);
		subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS
				| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
		return super.onCreateOptionsMenu(menu);
	}

}